<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/pravate/detail.css" />
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/mobile-detect.min.js" ></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../js/jquery-2.1.0.js"></script>
		<script type="text/javascript" src="../js/mobile-detect.min.js"></script>
		<script type="text/javascript" src="../js/config.js"></script>
		<script type="text/javascript" src="../js/template-web.js"></script>
		<!--<script type="text/javascript" src="../controll/detail.js" ></script>-->
	</head>
	<style type="text/css">
		.gl{
			padding: 10px 15px;
			font-size: 14px;
			color: #666;
		}
		.historyDe{
			color: #333;
			padding: 10px 15px;
			background: #fff;
			font-size: 14px;
		}
		.hidden{
			display: none;
		}
	</style>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">交易详情</h1>
			<span class="refund hidden">退款</span>
		</header>
		<div class="mui-content">
			
			<script type="text/html" id="model">
				{{if status==1}}
				<div class="detail_logo">
					<img src="{{imageN(data.pay_user)}}" />
					<p class="status">{{data.pay_status_txt}}</p>
					<p class="money">￥<span>{{data.total_amount}}</span></p>
				</div>
				<ul class="mui-table-view detail" style="padding-bottom: 2px;">
					<li class="mui-table-view-cell">收款时间<span class="time">{{data.created_at}}</span></li>
					<li class="mui-table-view-cell">订单号<span class="orderNum">{{data.out_trade_no}}</span></li>
					<li class="mui-table-view-cell">消费账户<span class="pay_account">{{pay_amount(data.buyer_id)}}</span></li>
					<li class="mui-table-view-cell">门店<span class="shopName">{{data.store_name}}</span></li>
					<li class="mui-table-view-cell">电话<span class="phone">{{data.store_phone}}</span></li>
					<li class="mui-table-view-cell">付款方式<span class="cashier">{{data.pay_user}}</span></li>
					<li class="mui-table-view-cell">备注<span class="bz">{{data.remark}}</span></li>
				</ul>
				{{if (data.RelationRecord&&data.RelationRecord.length>0)}}
					<div class="gl">
						查看关联订单
					</div>
					<div class="historyDe historyHas" data-id="{{data.RelationRecord[0].out_trade_no}}">{{data.RelationRecord[0].fina_type_txt}}-{{data.RelationRecord[0].out_trade_no}}<span style="float: right;">{{data.RelationRecord[0].total_amount}} <i class="mui-icon mui-icon-arrowright"></i></span></div>
				{{/if}}
				<button type="button" class="mui-btn mui-btn-warning mui-btn-block dayin" style="margin: 10px auto;width: 80%;padding: 6px 12px;">打印小票</button>
				<button type="button" class="mui-btn mui-btn-success mui-btn-block codeDayin" style="margin: 10px auto;width: 80%;padding: 6px 12px;">打印开票二维码</button>
				{{else}}
				<div class="detail_logo">
					<img src="../img/dp.png" />
					<p class="status"></p>
					<p class="money">￥--</p>
				</div>
				<ul class="mui-table-view detail" style="padding-bottom: 2px;">
					<li class="mui-table-view-cell">收款时间<span class="time"></span></li>
					<li class="mui-table-view-cell">订单号<span class="orderNum"></span></li>
					<li class="mui-table-view-cell">消费账户<span class="pay_account"></span></li>
					<li class="mui-table-view-cell">门店<span class="shopName"></span></li>
					<li class="mui-table-view-cell">电话<span class="phone"></span></li>
					<li class="mui-table-view-cell">付款方式<span class="cashier"></span></li>
					<li class="mui-table-view-cell">备注<span class="bz"></span></li>
					<button type="button" class="mui-btn mui-btn-warning mui-btn-block dayin" style="margin: 10px auto;width: 80%;padding: 6px 12px;">打印小票</button>
					<button type="button" class="mui-btn mui-btn-success mui-btn-block codeDayin" style="margin: 10px auto;width: 80%;padding: 6px 12px;">打印开票二维码</button>
				</ul>
				{{/if}}
			</script>
		</div>

		<script>
			mui.init();
			mui.plusReady(function() {
				document.addEventListener("DOMContentLoaded", function(){
					console.log(1);
				});
				var resData = {};
				//图片函数
				template.defaults.imports.imageN = function(payType) {
					var sta = "";
					if(payType == "支付宝") {
						sta = "../img/zhifubao.png";
					} else if(payType == "微信") {
						sta = "../img/weixin.png";
					} else if(payType == "银行卡") {
						sta = "../img/SfBank.png";
					}else{
						sta = "../img/dp.png";
					}
					return sta;
				}
				//图片函数
				template.defaults.imports.pay_amount = function(e) {
					var sta = "";
					e = e + "";
					if(e == "" || e == "undefined" || e == "null") {
						sta = "******";
					} else {
						sta = e;
					}
					return sta;
				}
				//状态函数
				template.defaults.imports.payStatus = function(numb) {
					var sta = ""
					switch(numb) {
						case 1:
							sta = "支付成功";
							break;
						case 2:
							sta = "支付取消";
							break;
						case 3:
							sta = "等待支付";
							break;
						case 4:
							sta = "已退款";
							break;
						case 5:
							sta = "订单关闭";
							break;
					}
					return sta;
				}
				plus.nativeUI.showWaiting("数据加载中，请勿退出...");
				var wx = plus.webview.currentWebview();
				console.log(wx.orderNum);
				console.log(wx.orderNum);
				require(weburl + model3, "json", "post", {
					"token": localStorage.getItem('token'),
					"out_trade_no": wx.orderNum,
					"auth_no": wx.orderNum
				}, function(response) {
					console.log(JSON.stringify(response));
					plus.nativeUI.closeWaiting();
					if (response.status == 0) {
						mui.alert(response.msg, " ", "关闭", function(e) {}, "div");
						return false;
					}
					if(response.data.pay_status==1){
						$(".refund").removeClass("hidden");
					}
					resData = response;
					var html = template("model", response);
					$(".mui-content").html(html);
					console.log(JSON.stringify(response));
				}, function(e) {
					plus.nativeUI.closeWaiting();
					var html = template("model", e);
					$(".mui-content").html(html);
					mui.toast('网络错误，请检查您的网络是否正确', {
						duration: 'long',
						type: 'div'
					});
					console.log("错误");
				})
				//点击退款
				mui("body").on("tap", ".refund", function() {
					mui.openWindow({
						url: "../view/refund.html",
						id: "refund",
						extras: {
							orderNum: wx.orderNum,
							refundPrice: $(".money span").html()
						}
					});
				})
				
				// 调用摄像头
				mui('body').on('tap', '.scanCode', function () {
					scanCode();
				})
				
				//点击了关联订单
				mui("body").on("tap",".historyHas",function(){
					var that=$(this);
					mui.openWindow({
						url: "./historyDetail.html",
						id: "historyDetail.html",
						extras: {
							orderNum: that.attr("data-id")
						}
					});
				})

				//点击打印小票
				var signx = false;
				mui("body").on("tap", ".dayin", function() {
					if(resData && resData.status == 1) {
//						var str = resData.data.print_body;
//						var reg = /\\n/g;
//						str = str.replace(reg, '\n');
//						mui.closePopups();
						mui.confirm("打印小票？", " ", ["打印", "关闭"], function(e) {
							console.log(JSON.stringify(e));
							if(e.index == 0) {
								if(signx == false) {
									signx = true;
									console.log(resData.data.print_array);
									printTicket(resData.data.print_array);
//									ticket(resData.data.store_name, str, resData.data.out_trade_no);
									setTimeout(function() {
										signx = false;
									}, 3000);
								}
								return false;
							}
						}, "div");
					} else {
						mui.alert("打印订单失败", " ", "关闭", function(e) {}, "div");
					}
				})

				//点击发票二维码
				var signxs = false;
				mui("body").on("tap", ".codeDayin", function() {
					mui.closePopups();
					mui.confirm("打印发票二维码？", " ", ["打印", "关闭"], function(e) {
						if(e.index == 0) {
							if(signxs == false) { 
								signxs = true;
								Bluetooth();
								require(weburl + model3, "json", "post", {
									"token": localStorage.getItem('token'),
									"out_trade_no": wx.orderNum,
									"auth_no": wx.authNo,
									"Invoice": 1
								}, function(response) {
									console.log(JSON.stringify(response));
									if(response.status==1){
										var str = response.data.print_body;
										var reg = /\\n/g;
										str = str.replace(reg, '\n');
										printTicket(response.data.print_array, 1);
//										fapiao_ticket(response.data.store_name, str, response.data.Invoice_url);
										return false;
									}
									mui.toast(response.data, {
										duration: 'long',
										type: 'div'
									});
								}, function(e) {
									var html = template("model", e);
									$(".mui-content").html(html);
									mui.toast('网络错误，请检查您的网络是否正确', {
										duration: 'long',
										type: 'div'
									});
								})
								setTimeout(function() {
									signxs = false;
								}, 3000);
							}
							return false;
						}
					},"div");

				})
			})
		</script>
	</body>

</html>